package com.hw.paltfrom.service.doncutor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.exception.HWSystemException;
import com.hw.paltfrom.mapper.*;
import com.hw.paltfrom.pack.ResponseResult;
import com.hw.paltfrom.pojo.*;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.PageUtils;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

/**
 * 指挥调度(市级) 梁天 2019年11月7日
 */
@Service
public class CityCommandTaskService {

	@Autowired
	HwCommandTaskMapper hwCommandTaskMapper;
	@Autowired
	HwDataOverrunstationOverweightMapper dataOverrunstationOverweightMapper;
	@Autowired
	HwSupervisionMapper hwSupervisionMapper;
	@Autowired
	private HwBaseAnnexesfileMapper hwBaseAnnexesfileMapper;
	@Autowired
	private CommandWorkflowService commandWorkflowService;
	@Autowired
	private BaseWorkflowService baseWorkflowService;
	@Autowired
	private HwCaseMainMapper hwCaseMainMapper;
	@Autowired
	private HwCommandTaskfeedbackMapper hwCommandTaskfeedbackMapper;
	@Autowired
	private HwCaseSrccomMapper hwCaseSrccomMapper;
	@Autowired
	private HwCasePoliceoperMapper hwCasePoliceoperMapper;
	@Autowired
	private HwBaseCountycoreMapper hwBaseCountycoreMapper;
	@Autowired
	HwBaseOverrunstationMapper hwBaseOverrunstationMapper;

	// 申请任务查询
	public Map<String, Object> sqquery(CommandTaskCityVO vo) {
		// 分页
		Integer pageNum = vo.getPageNum();
		Integer pageSize = vo.getPageSize();

		// 筛选条件
		String flowStage = vo.getFlowStage();

		String cph = vo.getCph();

		String taskType = vo.getTaskType();

		String areacode = vo.getfAreacode();

		String overrunCode = vo.getfFromdepname();

		EntityWrapper<HwDataOverrunstationOverweight> weightWrapper = new EntityWrapper<>();
		if (StringUtils.isNotBlank(cph)) {
			weightWrapper.like("CPH", cph);
		}

		if (StringUtils.isNotBlank(overrunCode)) {
			weightWrapper.eq("F_OverrunCode", overrunCode);
		}

		if (StringUtils.isNotBlank(areacode)) {
			weightWrapper.eq("F_AreaCode", areacode);
		}

		weightWrapper.orderBy("D_CreateTime", false);
		// 筛选后的称重表
		List<HwDataOverrunstationOverweight> overList = dataOverrunstationOverweightMapper.selectList(weightWrapper);
		// 返回数据集合
		ArrayList<CommandTaskCityVO> resultList = new ArrayList<>();
		PageUtils<CommandTaskCityVO> pageUtils = new PageUtils<>(pageNum, pageSize);
		for (HwDataOverrunstationOverweight orow : overList) {

			CommandTaskCityVO resultVo = new CommandTaskCityVO();

			// 查询当前称重数据 是否有对应的调度数据
			EntityWrapper<HwCommandTask> taskWrapper = new EntityWrapper<>();

			if (StringUtils.isNotBlank(taskType)) {
				taskWrapper.eq("Task_Type", taskType);
			}

			if (StringUtils.isNotBlank(flowStage)) {
				taskWrapper.eq("Flow_Stage", flowStage);
			}
			taskWrapper.eq("F_Id", orow.getfId()).eq("Task_Class", "1");
			List<HwCommandTask> CommandTaskList = hwCommandTaskMapper.selectList(taskWrapper);

			HwCommandTask oldCommandTask = null;
			if (!CommandTaskList.isEmpty()) {
				oldCommandTask = CommandTaskList.get(0);
			}

			if (oldCommandTask != null) {

				String oldflowStage = oldCommandTask.getFlowStage();
				String oldtaskType = oldCommandTask.getTaskType();

				// 按钮显示
				// 督导Flow_Stage='02'）的指挥调度任务
				if (!oldflowStage.equals("08")) {
					resultVo.setShowD(1);
				}
				// 上报视频
				if (oldflowStage.equals("07") && (oldtaskType.equals("02") || oldtaskType.equals("03"))) {
					resultVo.setShowR(1);
				}

			}

			resultVo.setfId(orow.getfId());
			resultVo.setfOverrunname(orow.getfOverrunname());
			resultVo.setfOverruncode(orow.getfOverruncode());
			resultVo.setCph(orow.getCph());
			resultVo.setdZs(orow.getdZs());
			resultVo.setdZz(orow.getdZz());
			resultVo.setdCxl(orow.getdCxl());
			resultVo.setdJcsj(orow.getdJcsj());
			resultVo.setjZz(orow.getjZz());
			resultVo.setjCxds(orow.getjCxds());
			resultVo.setjJcsj(orow.getjJcsj());
			resultVo.setfAreacode(orow.getfAreacode());
			if (oldCommandTask != null) {
				resultVo.setTaskType(oldCommandTask.getTaskType());
				resultVo.setFlowStage(oldCommandTask.getFlowStage());
				resultVo.setTaskId(oldCommandTask.getTaskId());
				resultVo.setFlowCode(oldCommandTask.getFlowCode());
				resultVo.setTaskClass(oldCommandTask.getTaskClass());

			}

			// 20191124新增规则
			// 如果未分类(调度没数据)
			if (oldCommandTask == null) {
				// 未查扣

				if (StringUtils.isNotBlank(taskType)) {
					String flag = resultVo.getTaskType();
					if (StringUtils.isNotBlank(flag)) {
						resultList.add(resultVo);
					}
				} else {

					if (orow.getIsSeized() == 0) {
						
						   // a按fid查的指挥调度表，如果查询条件为执行中，那么flowstage=08的应该不添加到返回集合
			            if (flowStage == null) {
			                // 执行中
			                if (!"08".equals(resultVo.getFlowStage())) {
			                    resultList.add(resultVo);
			                }
			            } else {
			                // 已完结
			                if ("08".equals(resultVo.getFlowStage())) {
			                    resultList.add(resultVo);
			                }

			            }
						
					}
					
				
					
				}
			} else {

				// a按fid查的指挥调度表，如果查询条件为执行中，那么flowstage=08的应该不添加到返回集合
				if (flowStage == null) {
					// 执行中
					if (!"08".equals(resultVo.getFlowStage())) {
						resultList.add(resultVo);
					}
				} else {
					// 已完结
					if ("08".equals(resultVo.getFlowStage())) {
						resultList.add(resultVo);
					}

				}

			}

		}

		Object info = pageUtils.getPageResult(resultList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);

	}

	// 指派任务查询
	public Map<String, Object> zpquery(CommandTaskCityVO vo) {

		// 分页
		Integer pageNum = vo.getPageNum();
		Integer pageSize = vo.getPageSize();

		// 筛选条件

		// 完结、 执行中
		String flowStage = vo.getFlowStage();
		// 地区
		String fareacode = vo.getfAreacode();
		// 发起单位
		String fFromdepname = vo.getfFromdepname();
		// 描述
		String taskContent = vo.getTaskContent();
		Timestamp StartTime = null;
		Timestamp EndTime = null;

		if (vo.getStartTime() == null) {
			if (vo.getEndTime() == null) {
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		} else {
			if (vo.getEndTime() != null) {
				StartTime = Timestamp.valueOf(vo.getStartTime());
				EndTime = Timestamp.valueOf(vo.getEndTime());
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		}

		EntityWrapper<HwCommandTask> taskWrapper = new EntityWrapper<>();
		if (StringUtils.isNotBlank(taskContent)) {
			taskWrapper.like("Task_Content", taskContent);
		}

		if (StringUtils.isNotBlank(flowStage)) {
			taskWrapper.eq("Flow_Stage", flowStage);
		} else {
			taskWrapper.ne("Flow_Stage", "08");
		}

		if (StringUtils.isNotBlank(fareacode)) {
			taskWrapper.eq("F_AreaCode", fareacode);
		}

		if (StringUtils.isNotBlank(fFromdepname)) {
			taskWrapper.eq("F_FromDepName", fFromdepname);
		}

		if (StartTime != null && EndTime != null) {
			taskWrapper.between("F_CreateTime", StartTime, EndTime);
		}

		taskWrapper.eq("Task_Class", "2").orderBy("F_CreateTime", false);
		List<HwCommandTask> CommandTaskList = hwCommandTaskMapper.selectList(taskWrapper);

		PageUtils<CommandTaskCityVO> pageUtils = new PageUtils<>(pageNum, pageSize);
		ArrayList<CommandTaskCityVO> resultList = new ArrayList<>();
		for (HwCommandTask oldCommandTask : CommandTaskList) {
			CommandTaskCityVO resultVo = new CommandTaskCityVO();

			String oldflowStage = oldCommandTask.getFlowStage();
			String oldtaskType = oldCommandTask.getTaskType();
			Integer getfFromdeptype = oldCommandTask.getfFromdeptype();

			// 按钮显示
			if (getfFromdeptype == 1 && !oldflowStage.equals("08")) {
				resultVo.setShowDelete(1);
				resultVo.setShowUpdate(1);
			}
			if (getfFromdeptype == 1 && oldflowStage.equals("07")) {
				resultVo.setShowEnd(1);
			}
			if (!oldflowStage.equals("08")) {
				resultVo.setShowD(1);
			}

			resultVo.setAssignDepcode(oldCommandTask.getAssignDepcode());
			resultVo.setAssignDepname(oldCommandTask.getAssignDepname());
			resultVo.setTaskType(oldCommandTask.getTaskType());
			resultVo.setFlowStage(oldCommandTask.getFlowStage());
			resultVo.setTaskId(oldCommandTask.getTaskId());
			resultVo.setFlowCode(oldCommandTask.getFlowCode());
			resultVo.setTaskClass(oldCommandTask.getTaskClass());
			resultVo.setfFromdepcode(oldCommandTask.getfFromdepcode());
			resultVo.setfFromdeptype(oldCommandTask.getfFromdeptype());
			resultVo.setfFromdepname(oldCommandTask.getfFromdepname());
			resultVo.setfCreatetime(oldCommandTask.getfCreatetime());
			resultVo.setAssignAccount(oldCommandTask.getAssignAccount());
			resultVo.setAssignAccount2(oldCommandTask.getAssignAccount2());
			resultVo.setAssignAccounttel(oldCommandTask.getAssignAccounttel());
			resultVo.setAssignAccounttel2(oldCommandTask.getAssignAccounttel2());
			resultVo.setTaskContent(oldCommandTask.getTaskContent());
			resultList.add(resultVo);
		}

		Object info = pageUtils.getPageResult(resultList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);

	}

	/*
	 * public Map<String, Object> queryAllList(CommandTaskCityVO vo) {
	 *
	 * int taskClass = vo.getTaskClass();
	 *
	 * // 申请任务 String cph = vo.getCph(); String taskType = vo.getTaskType();
	 * String areaCode = vo.getfAreacode();
	 *
	 * // 是否完结 String flowStage = vo.getFlowStage();
	 *
	 * // 指派任务 String taskContent = vo.getTaskContent(); String beginFTime =
	 * vo.getBeginFTime(); String finishFTime = vo.getFinishFTime();
	 *
	 * Integer pageNum = vo.getPageNum(); Integer pageSize = vo.getPageSize();
	 *
	 * Wrapper<HwDataOverrunstationOverweight> carwrapper = new
	 * EntityWrapper<>(); Wrapper<HwCommandTask> taskwrapper = new
	 * EntityWrapper<>(); List<CommandTaskCityVO> commandTaskCityVOList = new
	 * ArrayList<>();
	 *
	 * if (StringUtils.isNotBlank(taskType)) { taskwrapper.eq("Task_Type",
	 * taskType); } if (StringUtils.isNotBlank(areaCode)) {
	 * taskwrapper.eq("F_AreaCode", areaCode); } if ("08".equals(flowStage)) {
	 * taskwrapper.eq("Flow_Stage", flowStage); }else {
	 * taskwrapper.ne("Flow_Stage", flowStage); } if
	 * (StringUtils.isNotBlank(cph)) { carwrapper.like("CPH", cph);
	 * taskwrapper.eq("Task_Class", taskClass).orderBy("F_CreateTime");
	 * List<HwDataOverrunstationOverweight> clist =
	 * dataOverrunstationOverweightMapper.selectList(carwrapper); // for
	 * (HwDataOverrunstationOverweight // hwDataOverrunstationOverweight :
	 * clist){ for (int i = 0; i < clist.size(); i++) {
	 *
	 * taskwrapper.eq("F_Id", clist.get(i).getfId());
	 *
	 * List<HwCommandTask> taskList =
	 * hwCommandTaskMapper.selectList(taskwrapper); CommandTaskCityVO
	 * commandTaskCityVO = new CommandTaskCityVO();
	 *
	 * for (int j = 0; j < taskList.size(); j++) {
	 * commandTaskCityVO.setfId(taskList.get(j).getfId());
	 * commandTaskCityVO.setTaskType(taskList.get(j).getTaskType());
	 * commandTaskCityVO.setFlowStage(taskList.get(j).getFlowStage());
	 * commandTaskCityVO.setShowD(0); commandTaskCityVO.setShowR(0);
	 *
	 * if (!"08".equals(commandTaskCityVO.getFlowStage())) {
	 * commandTaskCityVO.setShowD(1); }
	 *
	 * if ("07".equals(commandTaskCityVO.getFlowStage()) &&
	 * ("02".equals(commandTaskCityVO.getTaskType()) ||
	 * "03".equals(commandTaskCityVO.getTaskType()))) {
	 * commandTaskCityVO.setShowR(1); }
	 *
	 * } commandTaskCityVO.setfOverrunname(clist.get(i).getfOverrunname());
	 * commandTaskCityVO.setCph(clist.get(i).getCph());
	 * commandTaskCityVO.setdZs(clist.get(i).getdZs());
	 * commandTaskCityVO.setdZz(clist.get(i).getdZz());
	 * commandTaskCityVO.setdCxl(clist.get(i).getdCxl());
	 * commandTaskCityVO.setdJcsj(clist.get(i).getdJcsj());
	 * commandTaskCityVO.setjZz(clist.get(i).getjZz());
	 * commandTaskCityVO.setjCxds(clist.get(i).getjCxds());
	 * commandTaskCityVO.setjJcsj(clist.get(i).getjJcsj());
	 * commandTaskCityVOList.add(commandTaskCityVO); }
	 *
	 * } else {
	 *
	 * int fFromdeptype = 1;
	 *
	 * if (StringUtils.isNotBlank(taskContent)) {
	 * carwrapper.like("Task_Content", taskContent); } if
	 * (StringUtils.isNotBlank(beginFTime) &&
	 * StringUtils.isNotBlank(finishFTime)) {
	 *
	 * long beginlong = new Long(beginFTime); long finishlong = new
	 * Long(finishFTime); taskwrapper.and().between("F_CreateTime", new
	 * Date(beginlong), new Date(finishlong)); }
	 *
	 * taskwrapper.eq("Task_Class", taskClass).orderBy("F_CreateTime");
	 *
	 * List<HwCommandTask> taskList =
	 * hwCommandTaskMapper.selectList(taskwrapper); for (HwCommandTask
	 * hwCommandTask : taskList) { HwDataOverrunstationOverweight carList =
	 * dataOverrunstationOverweightMapper.selectById(hwCommandTask.getfId());
	 * CommandTaskCityVO commandTaskCityVO = new CommandTaskCityVO();
	 * commandTaskCityVO.setfId(hwCommandTask.getfId());
	 * commandTaskCityVO.setfOverrunname(carList.getfOverrunname());
	 * commandTaskCityVO.setCph(carList.getCph());
	 * commandTaskCityVO.setdZs(carList.getdZs());
	 * commandTaskCityVO.setdZz(carList.getdZz());
	 * commandTaskCityVO.setdCxl(carList.getdCxl());
	 * commandTaskCityVO.setdJcsj(carList.getdJcsj());
	 * commandTaskCityVO.setjZz(carList.getjZz());
	 * commandTaskCityVO.setjCxds(carList.getjCxds());
	 * commandTaskCityVO.setjJcsj(carList.getjJcsj());
	 * commandTaskCityVO.setTaskType(hwCommandTask.getTaskType());
	 * commandTaskCityVO.setFlowStage(hwCommandTask.getFlowStage());
	 * commandTaskCityVO.setShowD(0); commandTaskCityVO.setShowR(0);
	 *
	 * if (!"08".equals(commandTaskCityVO.getFlowStage())) {
	 * commandTaskCityVO.setShowD(1); }
	 *
	 * if ("07".equals(commandTaskCityVO.getFlowStage()) &&
	 * ("02".equals(commandTaskCityVO.getTaskType()) ||
	 * "03".equals(commandTaskCityVO.getTaskType()))) {
	 * commandTaskCityVO.setShowR(1); }
	 *
	 * commandTaskCityVO.setShowDelete(0); commandTaskCityVO.setShowUpdate(0);
	 * commandTaskCityVO.setShowEnd(0);
	 *
	 * if (taskClass == 2 && fFromdeptype == 1) { if (!"08".equals(flowStage)) {
	 * commandTaskCityVO.setShowDelete(1); commandTaskCityVO.setShowUpdate(1); }
	 * if ("07".equals(flowStage)) { commandTaskCityVO.setShowEnd(1); } }
	 *
	 * commandTaskCityVOList.add(commandTaskCityVO); }
	 *
	 * }
	 *
	 * PageHelper.startPage(pageNum, pageSize); PageInfo<CommandTaskCityVO> info
	 * = new PageInfo<>(commandTaskCityVOList); return
	 * ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info); }
	 */

	// 督导
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class,
			HWSystemException.class, })
	public Map<String, Object> superVision(CommandTaskCityVO vo) {

		// 创建基础工作流
		HwBaseWorkflow hwBaseWorkflow;

		// 查询下一步的工作流code
		hwBaseWorkflow = baseWorkflowService.getStartBaseWorkflow("JCDD");
		String nowFlowStage = hwBaseWorkflow.getWorkflowCode();
		String nowFlowStageName = hwBaseWorkflow.getWorkflowCodename();
		String nextFlowStage = hwBaseWorkflow.getWorkflowNextcode();
		String nextFlowStageName = hwBaseWorkflow.getWorkflowNextcodename();

		// 检查督导创建
		HwSupervision hwSupervision = new HwSupervision();
		String spvId = RandomNumberUtil.getNumber();
		hwSupervision.setfSpvid(spvId);

		if ("".equals(vo.getTaskId())) {
			hwSupervision.setfFromobjectid(vo.getfId());
			hwSupervision.setfFromsrc(5);
		} else {
			hwSupervision.setfFromobjectid(vo.getTaskId());
			hwSupervision.setfFromsrc(1);
		}

		hwSupervision.setfFromtype(vo.getfFromdepname());
		hwSupervision.setfFromdepcode(vo.getfFromdepcode());
		hwSupervision.setfFromuserid(vo.getfFromuserid());
		hwSupervision.setfFromusername(vo.getfFromusername());
		hwSupervision.setfFromtime(new Timestamp(System.currentTimeMillis()));
		hwSupervision.setfTitle(vo.getfTitle());
		hwSupervision.setfSpvcontent(vo.getfSpvcontent());
		hwSupervision.setfCurrentstep(nextFlowStage);
		hwSupervision.setfBacktype(vo.getfBackdeptype());
		hwSupervision.setfBackdepcode(vo.getfBackdepcode());
		hwSupervision.setfBackdepname(vo.getfBackdepname());
		hwSupervision.setfBackassignstate(0);
		hwSupervision.setIsStagestate(0);
		Integer flag = hwSupervisionMapper.insert(hwSupervision);
		if (flag != 1) {
			throw new HWSystemException();
		}

		// 创建各阶段审批操作过程
		HwCommandWorkflow hwCommandWorkflow = new HwCommandWorkflow();
		hwCommandWorkflow.setCommandworkflowId(RandomNumberUtil.getNumber());
		hwCommandWorkflow.setWorkflowModule(hwBaseWorkflow.getWorkflowModule());
		hwCommandWorkflow.setWorkflowType(hwBaseWorkflow.getWorkflowType());
		hwCommandWorkflow.setWorkflowCode(nowFlowStage);
		hwCommandWorkflow.setWorkflowCodename(nowFlowStageName);
		hwCommandWorkflow.setCommandworkflowButtonname(vo.getButtonName());
		hwCommandWorkflow.setfAccount(vo.getfAccount());
		hwCommandWorkflow.setfFromdepcode(vo.getfFromdepcode());
		hwCommandWorkflow.setfFromdepname(vo.getfFromdepname());
		hwCommandWorkflow.setCommandworkflowDatatime(new Date());
		hwCommandWorkflow.setCommandworkflowButtonname(vo.getButtonName());
		hwCommandWorkflow.setCommandworkflowContent(vo.getfSpvcontent());
		hwCommandWorkflow.setObjectId(spvId);
		hwCommandWorkflow.setWorkflowNextcode(nextFlowStage);
		hwCommandWorkflow.setWorkflowNextcodename(nextFlowStageName);
		// hwCommandWorkflow.setfAnnexes(vo.getTaskEndfile());
		// 向各阶段审批操作过程表插入数据
		boolean success = commandWorkflowService.addWorkflow(hwCommandWorkflow);
		if (!success) {
			throw new HWSystemException();
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	// 指派任务完结
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class,
			HWSystemException.class, })
	public Map<String, Object> taskFinish(StepInfoVO vo) {

		Integer doResult = vo.getWorkflowDoresult(); // 0 正常 1 退回
		String taskId = vo.getTaskId();
		String workflowCode = vo.getWorkflowCode();
		// 调度任务分类
		Integer taskClass = vo.getTaskClass();

		Integer fFromdeptype = vo.getfFromdeptype();

		Wrapper<HwCommandTask> wrapper = new EntityWrapper<>();
		wrapper.eq("Task_ID", taskId);
		HwCommandTask hwCommandTask = hwCommandTaskMapper.selectById(taskId);

		if (workflowCode.equals("07") && taskClass == 2 && hwCommandTask.getfAccount().equals(vo.getLoginUser())
				&& hwCommandTask.getfFromdeptype().equals(vo.getfType())
				&& hwCommandTask.getfFromdepcode().equals(vo.getDeptCode())) {

			String nextFlowStage = vo.getWorkflowNextcode();
			Integer isStagestate = vo.getIsStagestate();
			if (doResult == 0) {
				hwCommandTask.setTaskEndtime(new Timestamp(System.currentTimeMillis()));
				String fileId = RandomNumberUtil.getNumber();
				hwCommandTask.setTaskEndaccount(vo.getfAccount());
				hwCommandTask.setTaskEnddepcode(vo.getDeptCode());
				hwCommandTask.setTaskEnddepname(vo.getDeptName());
				hwCommandTask.setTaskContent(vo.getTaskContent());
				hwCommandTask.setTaskEndfile(fileId);
				hwCommandTask.setFlowStage(nextFlowStage);
				hwCommandTask.setIsStagestate(isStagestate);
				Integer update = hwCommandTaskMapper.update(hwCommandTask, wrapper);
				if (update != 1) {
					throw new HWSystemException();
				}
			} else {
				hwCommandTask.setFlowStage(nextFlowStage);
				hwCommandTask.setIsStagestate(isStagestate);
				Integer update = hwCommandTaskMapper.update(hwCommandTask, wrapper);
				if (update != 1) {
					throw new HWSystemException();
				}
			}

			hwCommandTask.setFlowStage(vo.getWorkflowNextcode());
			Integer update = hwCommandTaskMapper.update(hwCommandTask, wrapper);
			if (update != 1) {
				throw new HWSystemException();
			}

			// 附件表新增
			String folderId = RandomNumberUtil.getNumber();
			List<FileInfo> fileInfoList = vo.getFileInfoList();
			int i = 0;
			for (FileInfo fileInfo : fileInfoList) {
				++i;
				HwBaseAnnexesfile hwBaseAnnexesfile = new HwBaseAnnexesfile();
				hwBaseAnnexesfile.setFileId(RandomNumberUtil.getNumber());
				hwBaseAnnexesfile.setfFolderid(folderId);
				hwBaseAnnexesfile.setFileType(fileInfo.getFileType());
				hwBaseAnnexesfile.setFileName("完结附件" + String.valueOf(i));
				hwBaseAnnexesfile.setFilePath(fileInfo.getFilePath());
				hwBaseAnnexesfile.setFileSize(fileInfo.getFileSize());
				hwBaseAnnexesfile.setFileMoudletype("ZHDDFILE指挥调度");
				hwBaseAnnexesfile.setfCreatedate(new Timestamp(System.currentTimeMillis()));
				hwBaseAnnexesfile.setfCreateusername(vo.getfAccount());
				Integer insert = hwBaseAnnexesfileMapper.insert(hwBaseAnnexesfile);
				if (insert != 1) {
					return ResponeResultUtil.fail(HWSystemCode.FAIL);
				}
			}

			// 创建各阶段审批操作过程
			HwCommandWorkflow hwCommandWorkflow = new HwCommandWorkflow();
			hwCommandWorkflow.setCommandworkflowId(RandomNumberUtil.getNumber());
			hwCommandWorkflow.setWorkflowModule(vo.getWorkflowModule());
			hwCommandWorkflow.setWorkflowType(vo.getWorkflowType());
			hwCommandWorkflow.setWorkflowCode(vo.getWorkflowCode());
			hwCommandWorkflow.setWorkflowCodename(vo.getWordflowCodename());
			hwCommandWorkflow.setCommandworkflowButtonname(vo.getButtonName());
			hwCommandWorkflow.setfAccount(vo.getLoginUser());
			hwCommandWorkflow.setfFromdepcode(vo.getDeptCode());
			hwCommandWorkflow.setfFromdepname(vo.getDeptName());
			hwCommandWorkflow.setCommandworkflowDatatime(new Date());
			hwCommandWorkflow.setCommandworkflowContent(vo.getContent());
			hwCommandWorkflow.setfAnnexes(folderId);
			hwCommandWorkflow.setObjectId(taskId);
			hwCommandWorkflow.setWorkflowNextcode(vo.getWorkflowNextcode());
			hwCommandWorkflow.setWorkflowNextcodename(vo.getWorkflowNextname());
			// 向各阶段审批操作过程表插入数据
			boolean success = commandWorkflowService.addWorkflow(hwCommandWorkflow);
			if (!success) {
				throw new HWSystemException();
			}

			return ResponeResultUtil.success(HWSystemCode.SUCCESS);

		} else {
			return ResponeResultUtil.fail(HWSystemCode.THE_FOUNDER_OF);
		}

	}

	// 指派任务删除
	public Map<String, Object> taskDelete(CommandTaskCityVO vo) {

		String taskId = vo.getTaskId();
		String flowStage = vo.getFlowStage();
		// 调度任务分类
		Integer taskClass = vo.getTaskClass();
		// 市级创建
		Integer fFromdeptype = vo.getfFromdeptype();

		Wrapper<HwCommandTask> wrapper = new EntityWrapper<>();
		wrapper.eq("Task_ID", taskId);
		HwCommandTask hwCommandTask = hwCommandTaskMapper.selectById(taskId);
		if (!"08".equals(flowStage) && taskClass == 2 && fFromdeptype == 1
				&& hwCommandTask.getfAccount().equals(vo.getfUserAccount())
				&& hwCommandTask.getfFromdeptype().equals(vo.getfType())
				&& hwCommandTask.getfFromdepcode().equals(vo.getfTypecode())) {

			Integer delete = hwCommandTaskMapper.deleteById(taskId);
			if (delete != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
			return ResponeResultUtil.success(HWSystemCode.SUCCESS);
		} else {
			return ResponeResultUtil.fail(HWSystemCode.THE_FOUNDER_OF);
		}
	}

	// 指派任务修改
	public Map<String, Object> taskUpdate(CommandTaskCityVO vo) {

		String taskId = vo.getTaskId();
		String flowStage = vo.getFlowStage();
		// 调度任务分类
		Integer taskClass = vo.getTaskClass();

		Wrapper<HwCommandTask> wrapper = new EntityWrapper<>();
		wrapper.eq("Task_ID", taskId);
		HwCommandTask hwCommandTask = hwCommandTaskMapper.selectById(taskId);
		if (!"08".equals(flowStage) && taskClass == 2 && hwCommandTask.getfFromdeptype().equals(vo.getfFromdeptype())
				&& hwCommandTask.getfAccount().equals(vo.getfUserAccount())
				&& hwCommandTask.getfFromdepcode().equals(vo.getfTypecode())) {

			HwCommandTask hwCommandTask1 = new HwCommandTask();
			hwCommandTask1.setAssignAccount(vo.getAssignAccount());
			hwCommandTask1.setAssignAccount2(vo.getAssignAccount2());
			hwCommandTask1.setAssignAccountname(vo.getAssignAccountname());
			hwCommandTask1.setAssignAccountname2(vo.getAssignAccountname2());
			hwCommandTask1.setAssignAccounttel(vo.getAssignAccounttel());
			hwCommandTask1.setAssignAccounttel2(vo.getAssignAccounttel2());
			hwCommandTask1.setAssignDepcode(vo.getAssignDepcode());
			hwCommandTask1.setAssignState(0);
			hwCommandTask1.setfId(vo.getfId());
			hwCommandTask1.setfAreacode(vo.getfAreacode());
			hwCommandTask1.setfAccount(vo.getfAccount());
			hwCommandTask1.setTaskClass(2);
			hwCommandTask1.setTaskType(vo.getTaskType());
			hwCommandTask1.setTaskContent(vo.getTaskContent());
			hwCommandTask1.setfFromdeptype(1);
			hwCommandTask1.setfFromdepname(vo.getfFromdepname());
			/*
			 * hwCommandTask1.setFlowCode("SJJB");
			 * hwCommandTask1.setFlowStage("07");
			 */

			Integer update = hwCommandTaskMapper.update(hwCommandTask1, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
			return ResponeResultUtil.success(HWSystemCode.SUCCESS);
		} else {
			return ResponeResultUtil.fail(HWSystemCode.THE_FOUNDER_OF);
		}
	}

	// 指派任务创建
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class,
			HWSystemException.class, })
	public Map<String, Object> taskAdd(CommandTaskCityVO vo) {

		// 创建基础工作流
		HwBaseWorkflow hwBaseWorkflow;

		// 查询下一步的工作流code
		hwBaseWorkflow = baseWorkflowService.getStartBaseWorkflow("JCDD");
		String nowFlowStage = hwBaseWorkflow.getWorkflowCode();
		String nowFlowStageName = hwBaseWorkflow.getWorkflowCodename();
		String nextFlowStage = hwBaseWorkflow.getWorkflowNextcode();
		String nextFlowStageName = hwBaseWorkflow.getWorkflowNextcodename();

		HwCommandTask hwCommandTask = new HwCommandTask();
		String taskId = RandomNumberUtil.getNumber();
		hwCommandTask.setTaskId(taskId);
		hwCommandTask.setAssignDepcode(vo.getAssignDepcode());
		hwCommandTask.setAssignDepname(vo.getAssignDepname());
		hwCommandTask.setAssignState(0);
		// hwCommandTask.setAssignStatetime(new
		// Timestamp(System.currentTimeMillis()));
		hwCommandTask.setfId("0"); // 存疑
		hwCommandTask.setfAccount(vo.getfAccount());
		hwCommandTask.setTaskClass(2);
		hwCommandTask.setTaskType(vo.getTaskType());
		hwCommandTask.setTaskContent(vo.getTaskContent());
		hwCommandTask.setfAreacode(vo.getfAreacode());
		hwCommandTask.setfCreatetime(new Timestamp(System.currentTimeMillis()));
		hwCommandTask.setfFromdeptype(1);
		hwCommandTask.setfFromdepname(vo.getfFromdepname());
		hwCommandTask.setfFromdepcode(vo.getfFromdepcode());
		hwCommandTask.setFlowCode("SJJB");
		hwCommandTask.setIsStagestate(0);

		HwBaseWorkflow startBaseWorkflow = baseWorkflowService.getStartBaseWorkflow("SJJB");
		hwCommandTask.setFlowStage(startBaseWorkflow.getWorkflowNextcode());

		Integer insert = hwCommandTaskMapper.insert(hwCommandTask);
		if (insert != 1) {
			throw new HWSystemException();
		}

		// 创建各阶段审批操作过程
		HwCommandWorkflow hwCommandWorkflow = new HwCommandWorkflow();
		hwCommandWorkflow.setCommandworkflowId(RandomNumberUtil.getNumber());
		hwCommandWorkflow.setWorkflowModule(startBaseWorkflow.getWorkflowModule());
		hwCommandWorkflow.setWorkflowType("SJJB");
		hwCommandWorkflow.setWorkflowCode(nowFlowStage);
		hwCommandWorkflow.setWorkflowCodename(startBaseWorkflow.getWorkflowCodename());
		hwCommandWorkflow.setCommandworkflowButtonname(vo.getButtonName());
		hwCommandWorkflow.setfAccount(vo.getfAccount());
		hwCommandWorkflow.setfFromdepcode(vo.getfFromdepcode());
		hwCommandWorkflow.setfFromdepname(vo.getfFromdepname());
		hwCommandWorkflow.setCommandworkflowDatatime(new Date());
		// hwCommandWorkflow.setCommandworkflowContent(vo.getTaskContent());
		hwCommandWorkflow.setObjectId(taskId);
		hwCommandWorkflow.setWorkflowNextcode(startBaseWorkflow.getWorkflowNextcode());
		hwCommandWorkflow.setWorkflowNextcodename(startBaseWorkflow.getWorkflowNextcodename());
		// 向各阶段审批操作过程表插入数据
		boolean success = commandWorkflowService.addWorkflow(hwCommandWorkflow);
		if (!success) {
			throw new HWSystemException();
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	// 上报视频
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class,
			HWSystemException.class })
	public Map<String, Object> reportTheVideo(CommandTaskCityVO vo) {

		Wrapper<HwCommandTask> wrapper = new EntityWrapper<>();
		wrapper.eq("Task_ID", vo.getTaskId());
		HwCommandTask hwCommandTask1 = hwCommandTaskMapper.selectById(vo.getTaskId());
		// 当前阶段
		String flowStage = hwCommandTask1.getFlowStage();
		String flowCode = hwCommandTask1.getFlowCode();

		// 判断是否满足条件
		if (!"07".equals(flowStage) && ("03".equals(vo.getTaskType()) || "02".equals(vo.getTaskType()))) {
			return ResponeResultUtil.fail(HWSystemCode.REPORT_THE_VIDEO);
		} else {
			// 附件表新增
			List<FileInfo> fileInfoList = vo.getFileInfoList();
			int i = 0;
			for (FileInfo fileInfo : fileInfoList) {
				++i;
				HwBaseAnnexesfile hwBaseAnnexesfile = new HwBaseAnnexesfile();
				hwBaseAnnexesfile.setFileMoudletype("ZHDDFILE指挥调度");
				hwBaseAnnexesfile.setFileName("视频附件" + String.valueOf(i));
				hwBaseAnnexesfile.setFileId(RandomNumberUtil.getNumber());
				hwBaseAnnexesfile.setfCreatedate(new Timestamp(System.currentTimeMillis()));
				hwBaseAnnexesfile.setfCreateusername(vo.getfCreateusername());
				hwBaseAnnexesfile.setfFolderid(RandomNumberUtil.getNumber());
				hwBaseAnnexesfile.setFilePath(fileInfo.getFilePath());
				hwBaseAnnexesfile.setFileSize(fileInfo.getFileSize());
				hwBaseAnnexesfile.setFileType("ZHDDFILE");

				Integer insert = hwBaseAnnexesfileMapper.insert(hwBaseAnnexesfile);
				if (insert != 1) {
					throw new HWSystemException();
				}

			}
			HwCommandTask hwCommandTask = new HwCommandTask();
			hwCommandTask.setTaskType(vo.getTaskType());
			// 创建基础工作流
			HwBaseWorkflow hwBaseWorkflow = baseWorkflowService.getPresentWorkflow(flowStage, flowCode);
			hwCommandTask.setFlowStage(hwBaseWorkflow.getWorkflowNextcode());
			hwCommandTask.setTaskVideofile(vo.getfFolderid());
			Integer update = hwCommandTaskMapper.update(hwCommandTask, wrapper);
			if (update != 1) {
				throw new HWSystemException();
			}

			// 创建各阶段审批操作过程
			HwCommandWorkflow hwCommandWorkflow = new HwCommandWorkflow();
			hwCommandWorkflow.setCommandworkflowId(RandomNumberUtil.getNumber());
			hwCommandWorkflow.setWorkflowModule(hwBaseWorkflow.getWorkflowModule());
			hwCommandWorkflow.setWorkflowType(flowCode);
			hwCommandWorkflow.setWorkflowCode(flowStage);
			hwCommandWorkflow.setWorkflowCodename(hwBaseWorkflow.getWorkflowCodename());
			hwCommandWorkflow.setCommandworkflowButtonname(vo.getButtonName());
			hwCommandWorkflow.setfAccount(vo.getfAccount());
			hwCommandWorkflow.setfFromdepcode(vo.getfFromdepcode());
			hwCommandWorkflow.setfFromdepname(vo.getfFromdepname());
			hwCommandWorkflow.setCommandworkflowDatatime(new Date());
			// hwCommandWorkflow.setCommandworkflowContent(vo.getTaskContent());
			hwCommandWorkflow.setObjectId(vo.getTaskId());
			hwCommandWorkflow.setfAnnexes(vo.getTaskVideofile());
			hwCommandWorkflow.setWorkflowNextcode(hwBaseWorkflow.getWorkflowNextcode());
			hwCommandWorkflow.setWorkflowNextcodename(hwBaseWorkflow.getWorkflowCodename());
			// 向各阶段审批操作过程表插入数据
			boolean success = commandWorkflowService.addWorkflow(hwCommandWorkflow);
			if (!success) {
				throw new HWSystemException();
			}
			return ResponeResultUtil.success(HWSystemCode.SUCCESS);
		}

	}

	// 查询市区和县区信息 -----指派任务
	public Map<String, Object> queryzpStation(CommandTaskCityVO vo) {

		// 为反馈单位提供市单位和所属县中心名
		HwCommandTask hwCommandTask = hwCommandTaskMapper.selectById(vo.getTaskId());

		EntityWrapper<HwBaseCountycore> countryWrapper = new EntityWrapper<>();
		countryWrapper.eq("F_AreaId", hwCommandTask.getfAreacode());
		List<HwBaseCountycore> hwBaseCountycore = hwBaseCountycoreMapper.selectList(countryWrapper);
		HwBaseWorkflow hwBaseWorkflow = baseWorkflowService.getPresentWorkflow(vo.getFlowStage(), vo.getFlowCode());
		CommandTaskCityVO resultVo = new CommandTaskCityVO();

		if ("SJJB".equals(vo.getFlowCode())) {
			if ("1".equals(hwBaseWorkflow.getWorkflowDepcodetype())) {
				resultVo.setDefautCode(vo.getfFromdepcode());
			} else {
				resultVo.setDefautCode(hwBaseCountycore.get(0).getfCountycorecode());
			}
		}
		if ("XJJB".equals(vo.getFlowCode())) {
			resultVo.setDefautCode(hwBaseCountycore.get(0).getfCountycorecode());
		}

		resultVo.setCityType("1");
		resultVo.setCountryType("2");
		resultVo.setCountryCode(hwBaseCountycore.get(0).getfCountycorecode());
		resultVo.setCountryName(hwBaseCountycore.get(0).getfCountycorename());
		resultVo.setCityCode(vo.getfFromdepcode());
		resultVo.setCityName(vo.getfFromdepname());

		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, resultVo);
	}

	// 查询站点和县区信息 -----申请任务
	public Map<String, Object> querysqStation(CommandTaskCityVO vo) {

		// 为反馈单位提供站点名和所属县中心名
		HwDataOverrunstationOverweight hwDataOverrunstationOverweight = dataOverrunstationOverweightMapper
				.selectById(vo.getfId());

		EntityWrapper<HwBaseCountycore> countryWrapper = new EntityWrapper<>();
		countryWrapper.eq("F_AreaId", vo.getfAreacode());
		List<HwBaseCountycore> hwBaseCountycore = hwBaseCountycoreMapper.selectList(countryWrapper);

		CommandTaskCityVO resultVo = new CommandTaskCityVO();

		if ("".equals(vo.getFlowStage()) && "".equals(vo.getFlowCode())) {
			resultVo.setDefautCode(hwDataOverrunstationOverweight.getfOverruncode());
		} else {
			HwBaseWorkflow hwBaseWorkflow = baseWorkflowService.getPresentWorkflow(vo.getFlowStage(), vo.getFlowCode());

			if ("2".equals(hwBaseWorkflow.getWorkflowDepcodetype())) {
				resultVo.setDefautCode(hwBaseCountycore.get(0).getfCountycorecode());
			} else {
				resultVo.setDefautCode(hwDataOverrunstationOverweight.getfOverruncode());
			}
		}

		resultVo.setCountryType("2");
		resultVo.setStationType("3");
		resultVo.setStationCode(hwDataOverrunstationOverweight.getfOverruncode());
		resultVo.setStationName(hwDataOverrunstationOverweight.getfOverrunname());
		resultVo.setCountryCode(hwBaseCountycore.get(0).getfCountycorecode());
		resultVo.setCountryName(hwBaseCountycore.get(0).getfCountycorename());

		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, resultVo);
	}

	// 查询调度信息和调度督导
	public Map<String, Object> queryDetail(CommandTaskCityVO vo) {
		HashMap<String, Object> result = new HashMap<>();
		String taskId = vo.getTaskId();

		// 基础信息
		HwCommandTask task = hwCommandTaskMapper.selectById(taskId);
		result.put("simpleData", task);
		// 基础信息附件

		// 情况说明附件
		String taskExplainfile = task.getTaskExplainfile();
		Wrapper<HwBaseAnnexesfile> qksmwrapper = new EntityWrapper<>();
		qksmwrapper.eq("F_FolderId", taskExplainfile);
		List<HwBaseAnnexesfile> qksm = hwBaseAnnexesfileMapper.selectList(qksmwrapper);
		ArrayList<HwBaseAnnexesfile> simpleDataFile = new ArrayList<>(qksm);
		// 视频附件
		String taskVideofile = task.getTaskVideofile();
		Wrapper<HwBaseAnnexesfile> spwrapper = new EntityWrapper<>();
		spwrapper.eq("F_FolderId", taskVideofile);
		List<HwBaseAnnexesfile> sq = hwBaseAnnexesfileMapper.selectList(spwrapper);
		simpleDataFile.addAll(sq);
		// 移交通知单附件
		String taskTransferfile = task.getTaskTransferfile();
		Wrapper<HwBaseAnnexesfile> yjtzdwrapper = new EntityWrapper<>();
		yjtzdwrapper.eq("F_FolderId", taskTransferfile);
		List<HwBaseAnnexesfile> yjtzd = hwBaseAnnexesfileMapper.selectList(yjtzdwrapper);
		simpleDataFile.addAll(yjtzd);
		// 完结附件
		String taskEndfile = task.getTaskEndfile();
		Wrapper<HwBaseAnnexesfile> wjwrapper = new EntityWrapper<>();
		wjwrapper.eq("F_FolderId", taskEndfile);
		List<HwBaseAnnexesfile> wj = hwBaseAnnexesfileMapper.selectList(wjwrapper);
		simpleDataFile.addAll(wj);
		result.put("simpleDataFile", simpleDataFile);

		Wrapper<HwCommandTaskfeedback> wrapper = new EntityWrapper<>();
		wrapper.eq("Task_ID", taskId);
		// 查询反馈信息
		ArrayList<Object> fkList = new ArrayList<>();
		List<HwCommandTaskfeedback> backList = hwCommandTaskfeedbackMapper.selectList(wrapper);
		for (HwCommandTaskfeedback fk : backList) {
			Map<String, Object> fkmap = (Map<String, Object>) JSON.toJSON(fk);
			ArrayList<HwBaseAnnexesfile> fkFile = new ArrayList<>();
			Map<String, Object> columnMap = new HashMap<>();

			// 车头照片
			String checkHeadpic = fk.getCheckHeadpic();
			if (StringUtils.isNotBlank(checkHeadpic)) {
				columnMap.put("F_FolderId", checkHeadpic);
				List<HwBaseAnnexesfile> selectByMap = hwBaseAnnexesfileMapper.selectByMap(columnMap);
				fkFile.addAll(selectByMap);
			}

			// 车尾照片
			String checkTailpic = fk.getCheckTailpic();
			if (StringUtils.isNotBlank(checkTailpic)) {
				columnMap.put("F_FolderId", checkTailpic);
				List<HwBaseAnnexesfile> selectByMap = hwBaseAnnexesfileMapper.selectByMap(columnMap);
				fkFile.addAll(selectByMap);
			}
			// 称重仪
			String checkWeightpic = fk.getCheckWeightpic();
			if (StringUtils.isNotBlank(checkWeightpic)) {
				columnMap.put("F_FolderId", checkWeightpic);
				List<HwBaseAnnexesfile> selectByMap = hwBaseAnnexesfileMapper.selectByMap(columnMap);
				fkFile.addAll(selectByMap);
			}
			// 现场抓拍
			String taskreturnPic = fk.getTaskreturnPic();
			if (StringUtils.isNotBlank(taskreturnPic)) {
				columnMap.put("F_FolderId", taskreturnPic);
				List<HwBaseAnnexesfile> selectByMap = hwBaseAnnexesfileMapper.selectByMap(columnMap);
				fkFile.addAll(selectByMap);
			}

			fkmap.remove("Check_HeadPic");
			fkmap.remove("Check_TailPic");
			fkmap.remove("Check_WeightPic");
			fkmap.remove("TaskReturn_Pic");
			fkmap.put("fkPic", fkFile);
			fkList.add(fkmap);
		}
		result.put("fkxx", fkList);

		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, result);

	}

	// 查询所有县治超中心(除去市)
	public Map<String, Object> queryAllCenter(CommandTaskCityVO vo) {

		String typeCode = vo.getfCountyCoreCode();
		Wrapper<HwBaseCountycore> wrapper = new EntityWrapper<>();
		wrapper.ne("F_CountyCoreCode", typeCode);
		wrapper.orderBy("F_SortCode");
		List<HwBaseCountycore> list = hwBaseCountycoreMapper.selectList(wrapper);

		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, list);

	}
}
